﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SupervisedSpeakerRecognition
{
    public class SilenceRedeuceCommand : ICommand
    {
        private double[] inputData;
        private double[] outputData;
        private double avg;
        private int frameSize;

        public double[] OutputData
        {
            get { return outputData; }
            set { outputData = value; }
        }
        public int FrameSize
        {
            get { return frameSize; }
            set { frameSize = value; }
        }

        public double[] InputData
        {
            get { return inputData; }
            set { inputData = value; }
        }

        public void execute()
        {

            double[] temp = new double[InputData.Length];
            int cnt = 0;
            //int startIndex =0,
             int stopIndex;
            stopIndex = inputData.Length;
            AVG();
            for (int i = 0; i < inputData.Length; i = i + frameSize)
            {
                if (frameAvg(i) > avg)
                {
                    for (int j = i; (j < frameSize + i && j < inputData.Length); j++)
                    {
                        temp[cnt++] = InputData[j];
                    }
                    //startIndex = i;
                    //break;
                }
            }
            //for (int i = inputData.Length-1; i >0; i--)
            //{
            //    if (inputData[i] > avg || inputData[i] * (-1) > avg)
            //    {

            //        stopIndex = i;
            //        break;
            //    }
            //}

            //outputData = new double[(stopIndex - startIndex)+1];
            //for (int i = startIndex,j=0; i < stopIndex; i++)
            //{
            //    outputData[j++] = inputData[i];

            //}

            outputData = new double[cnt];
            for (int i = 0; i < cnt; i++)
            {
                outputData[i] = temp[i];
            }

        }

        public void AVG()
        {
            double sum = 0;

            for (int i = 0; i < inputData.Length; i++)
            {
                if (inputData[i] < 0)
                {
                    sum += inputData[i] * (-1);
                }
                else
                {
                    sum += inputData[i];
                }
            }
            avg = sum / (inputData.Length);
        }

        private double frameAvg(int index)
        {
            double sum = 0, avg = 0;
            for (int i = index; (i < index + frameSize && i < inputData.Length); i++)
            {
                if (inputData[i] < 0)
                {
                    sum += inputData[i] * (-1);
                }
                else
                {
                    sum += inputData[i];
                }
            }
            avg = sum / (frameSize);
            return avg;
        }


    }
}
